<?php
class FieldBuilder
{
    public static function Build($fieldName, $props, $addValidators=true)
    {
        //print_r($props);
        $control = null;
        $defaultValue = isset($_POST[$fieldName]) ? $_POST[$fieldName] :
        (array_key_exists("default", $props) ? $props["default"] : "");
        $canValidated = true;
        if(is_array($props))
        {
            switch($props["type"])
            {
                case "integer":
                case "string":
                case "decimal":
                    $control = new textbox($defaultValue);
                    $control->setname($fieldName);
                    $control->setLabel($fieldName);
                    break;
                case "password":
                    $control = new passwordbox();
                    $control->setname($fieldName);
                    $control->setLabel($fieldName);
                    break;
                case "text":
                    $control = new textarea($defaultValue);
                    $control->setname($fieldName);
                    $control->setLabel($fieldName);
                    break;
                case "enum":
                case "select":
                    $control = new select();
                    $control->setname($fieldName);
                    $control->setLabel($fieldName);
                    if(!array_key_exists("isRelationField", $props))
                    {
                        foreach($props["values"] as $selectValue)
                        {
                            $control->addValue($selectValue, $selectValue);
                        }
                    }
                    else {
                        $entityTable = Doctrine::getTable($props["foreignTable"]);
                        $dataSource = array();
                        $fieldText = array_key_exists("fieldText", $props) ?
                                $props["fieldText"] : $props["foreignField"];
                        $fieldText = $entityTable->getFieldName($fieldText);
                        $fieldValue = array_key_exists("fieldText", $props) ?
                                $props["fieldValue"] : $props["foreignField"];
                        $fieldValue = $entityTable->getFieldName($fieldValue);
                        if($props["dataSource"] != null)
                            $dataSource = $props["dataSource"]->toArray();
                         else
                         {
                             $entities = $entityTable->findAll();
                             $dataSource = $entities->toArray();
                         }
                        self::fillFromDataSource($control, $dataSource,
                                $fieldText, $fieldValue);
                    }
                    $control->setSelected($defaultValue);
                    break;
            }
            if($canValidated)
            {
                if(!empty($props["notnull"]) && $addValidators)
                {
                    $length = array_key_exists("length", $props)
                            ? $props["length"] : 0;
                    self::addValidator($control, $props["type"], $length);
                }
                else
                    $control->skipValidation();
            }
        }
        return $control;
    }
    static private function addValidator(&$control, $type, $length=0)
    {
        switch ($type)
        {
            case "integer":
                $control->setValidRegex("/^\\d$/");
                break;
            case "decimal":
            case "float":
                $control->setValidRegex("/^(-)?\d+(\.\d\d)?$/");
                break;
            case "string":
            case "password":
                $control->setminLength(1);
                if($length > 0)
                    $control->setmaxLength($length);
                break;
        }
    }
    static private function fillFromDataSource(&$control, $dataSourceArray, $fieldText,
            $fieldValue)
    {
        foreach($dataSourceArray as $entity)
        {
            $control->addValue($entity[$fieldValue],
                    $entity[$fieldText]);
        }
    }
}
?>